package cn.fansunion.concurrent.threadabc;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//有3个线程ABC。按照ABC来运行（A线程输出A，B线程输出B，C线程输出C，以此类推，循环输出）。
public class ThreadABCTest4 {

	private static Lock lock = new ReentrantLock();

	public static void main(String[] args) {
		final int maxNum = 3;

		Thread aThread = new Thread(new Runnable() {

			@Override
			public void run() {
				for (int index = 0; index < maxNum; index++) {
					lock.lock();
					System.out.println("A");
					lock.unlock();
				}

			}

		});
		Thread bThread = new Thread(new Runnable() {

			@Override
			public void run() {
				for (int index = 0; index < maxNum; index++) {
					lock.lock();
					System.out.println("B");
					lock.unlock();
				}

			}

		});
		Thread cThread = new Thread(new Runnable() {

			@Override
			public void run() {
				for (int index = 0; index < maxNum; index++) {
					lock.lock();
					System.out.println("C");
					lock.unlock();
				}

			}

		});
		aThread.start();

		bThread.start();

		cThread.start();
	}

}
